package com.wbao.dianniu.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.IReleaseActiveAdapter;
import com.wbao.dianniu.data.ActivityData;
import com.wbao.dianniu.data.ActivityListData;
import com.wbao.dianniu.listener.IActivityListListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.ActivityListManager;
import com.wbao.dianniu.update.ReleaseDataManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户活动列表
 *
 * @author yejianjun
 */
public class HeadActFragment extends Fragment implements IActivityListListener, ReleaseDataManager.IReleaseDelListener {

    private ActivityListManager manager;
    private PullToRefreshListView pullListView;
    private ListView listView;
    private final int PAGE_COUNTS = 10;
    private int currentPage = 0;
    private List<ActivityListData> mList = new ArrayList<ActivityListData>();
    private IReleaseActiveAdapter adapter;
    private TextView noDataTV;
    private final int TYPE = 3;
    private String title = null;
    private static int friendId = 0;
    private ReleaseDataManager releaseDataManager;

    public static HeadActFragment instance(Context context, int id) {
        HeadActFragment view = new HeadActFragment();
        friendId = id;
        return view;
    }

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_i_release, null);
        initView(view);
        initData();
        releaseDataManager = ReleaseDataManager.getInstance();
        releaseDataManager.addListener(this);
        return view;
    }

    private void initView(View view) {
        noDataTV = view.findViewById(R.id.no_find_data);
        pullListView = view.findViewById(R.id.i_release_listview);
        pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
        pullListView.setOnRefreshListener(onRefreshListener);
        listView = pullListView.getRefreshableView();
        registerForContextMenu(listView);
    }

    private void initData() {
        manager = new ActivityListManager(getContext());
        manager.addActivityListener(this);
        adapter = new IReleaseActiveAdapter(getContext());
        adapter.addData(mList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(itemClickListener);
        reqData(0);
    }

    private void reqData(int page) {
        manager.activityReq(friendId, page * PAGE_COUNTS,
                PAGE_COUNTS, "", TYPE);
    }

    OnRefreshListener onRefreshListener = new OnRefreshListener() {

        @Override
        public void onRefresh() {
            // 上拉刷新 加载请求数据
            pullUpRequest();
        }
    };

    // 上拉刷新数据请求
    private void pullUpRequest() {
        // 没有搜索结果 或者数据为空 上拉没有效果
        currentPage++;
        reqData(currentPage);
    }

    OnItemClickListener itemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            // TODO Auto-generated method stub
            ActivityListData data = mList.get(position - 1);
            if (friendId == GlobalContext.getAccountId()) {    // 我的活动，跳转到报名管理
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString(Const.BUNDLE_BROWSE, data.getBrowseCount() + "");
                bundle.putString(Const.BUNDLE_SIGNUP, data.getInCount() + "");
                bundle.putString(Const.BUNDLE_PASS, data.getPassCount() + "");
                bundle.putInt(Const.BUNDLE_ID, data.getId());
                bundle.putString(Const.BUNDLE_ADDRESS, data.getAddress());
                bundle.putString(Const.BUNDLE_PRICE, data.getPrice() + "");
                bundle.putString(Const.BUNDLE_SUBJECT, data.getSubject());
                bundle.putString(Const.BUNDLE_MAXPERSION, data.getMaxPersion() + "");
                bundle.putString(Const.BUNDLE_STARTDATE, data.getStartDate());
                intent.putExtra(Const.BUNDLE_DATA, bundle);
                intent.setClass(getContext(), ActManagerActivity.class);
                startActivity(intent);
            } else {  //  查看其他人的活动，跳转到活动详情
                Intent rightIntent = new Intent();
                rightIntent.putExtra(Const.INTENT_ACTIVITYID, data.getId());
                rightIntent.setClass(getContext(), ActivityDetail.class);
                startActivity(rightIntent);
            }

        }
    };

    @Override
    public void removeOneData(int activityId) {
        for (int i = 0; i < mList.size(); i++) {
            if (activityId == mList.get(i).getId()) {
                pullListView.onRefreshComplete(false, false);
                mList.remove(i);
                mList = adapter.getList();
                if (adapter.getCount() == 0) {
                    noDataTV.setVisibility(View.VISIBLE);
                } else {
                    noDataTV.setVisibility(View.GONE);
                }
                adapter.notifyDataSetChanged();
                break;
            }
        }
    }

    @Override
    public void modifyOneData(int activityId, ActivityData data) {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getId() == data.id) {
                mList.get(i).setSubject(data.subject);
                mList.get(i).setStartDate(data.startDate);
                mList.get(i).setMaxPersion(data.maxPersion);
                mList.get(i).setAddress(data.address);
                mList.get(i).setPrice(data.price);
                adapter.notifyDataSetChanged();
                break;
            }
        }
    }

    @Override
    public void onActivityFailure(int errorCode, String errorMsg) {
        pullListView.onRefreshComplete(false, false);
    }

    @Override
    public void onActivitySuccess(List<ActivityListData> list) {
        pullListView.onRefreshComplete(false, false);
        if (null != list) {
            adapter.addData(list);
            mList = adapter.getList();
            if (adapter.getCount() == 0) {
                noDataTV.setVisibility(View.VISIBLE);
            } else {
                noDataTV.setVisibility(View.GONE);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != manager) {
            manager.removeActivityListener(this);
        }
        if (null != releaseDataManager) {
            releaseDataManager.removeListener(this);
        }
    }
}
